package com.sc.nft.task;

import com.sc.nft.dao.ActivityBasicsDao;
import com.sc.nft.dao.PrizeDrawActivityPoolSettleLogDao;
import com.sc.nft.entity.ActivityBasics;
import com.sc.nft.entity.PrizeDrawActivitySettleRecords;
import com.sc.nft.entity.UserPrizeDrawRecords;
import com.sc.nft.enums.PrizeDrawActivityTypeEnum;
import com.sc.nft.service.ActivityBasicsService;
import com.sc.nft.service.CarveUpPoolService;
import com.sc.nft.service.PrizeDrawResultService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 抽奖定时任务
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class PrizeDrawResultTask {

    private final PrizeDrawResultService prizeDrawResultService;

    private final CarveUpPoolService carveUpPoolService;

    private final PrizeDrawActivityPoolSettleLogDao prizeDrawActivityPoolSettleLogDao;
    private final ActivityBasicsService activityBasicsService;

    private final ActivityBasicsDao activityBasicsDao;


    /**
     * 排列5的当天开奖结果.
     * 执行时间放在晚上10点(22:00)进行执行
     *
     * @throws Exception
     */
    @XxlJob("arrangeFiveResult")
    public void arrangeFiveResult() {
        prizeDrawResultService.arrangeFiveResult();
    }


    /**
     * 奖池累计 每5分钟执行一次
     *
     * @throws Exception
     */
    @XxlJob("prizePoolAccumulation")
    public void prizePoolAccumulation() {
        carveUpPoolService.getPoolSettleLogList().forEach(prizeDrawActivityPoolSettleLog -> {

            carveUpPoolService.addPoolAmount(prizeDrawActivityPoolSettleLog);

            prizeDrawActivityPoolSettleLogDao.updateIssettleById(prizeDrawActivityPoolSettleLog.getId());
        });
    }


    /**
     * 竞猜开奖
     */
    @XxlJob("guessRunLottery")
    public void guessRunLottery() {
        // 把竞猜活动开奖未结算并且结束时间大于等于当天的数据获取到
        // 竞猜活动符合开奖条件数据
        List<ActivityBasics> dataThatMeetsLotteryConditions = activityBasicsDao.getDataThatMeetsLotteryConditions(2);
        dataThatMeetsLotteryConditions.forEach(activityBasics -> {
            prizeDrawResultService.drawLottery(activityBasics);
        });
    }
    /**
     * 瓜分活动开奖  开始时间 23:30
     *  特等奖：全部数字匹配
     * 一等奖：只需第2-5位数字匹配
     * 二等奖：只需第3-5位数字匹配
     * 三等奖：只需第4-5位数字匹配
     * 四等奖：只需第5位数字匹配
     *
     *   (初始奖池+累计奖池) = 总奖池
     *
     *  奖金分布 总奖池 * (100-预留配置)  * 奖项百分比  / 人数
     *
     *  若本轮未中奖，奖池自动累计到下一期
     *
     *   总奖池- ((初始奖池+累计奖池) * (100-预留配置)) 也累计到剩余奖池
     *
     */
    @XxlJob("divideUpTheLottery")
    public void DivideUpTheLottery() {
        activityBasicsService.getLotteryDataByType(PrizeDrawActivityTypeEnum.PARTITION_ACTIVITY).forEach(activityBasics -> {
            // 开奖
            prizeDrawResultService.divideUpTheLottery(activityBasics);
        });

    }

    /**
     * 中奖数据分发
     */
    @XxlJob("distributeWinningPrizes")
    public void distributeWinningPrizes() {
        List<PrizeDrawActivitySettleRecords> list = prizeDrawResultService.getListByIsSettleAndStatus();
        list.forEach(prizeDrawActivitySettleRecords -> {
            // 发放奖励
            prizeDrawResultService.distributeWinningPrizes(prizeDrawActivitySettleRecords);
        });
    }



}
